using System;
using System.Linq;
using System.Windows.Controls.Ribbon;
using Clancy.Ui.ViewModels;

namespace Clancy.Ui.Factories
{
    static class RibbonElementFactory
    {
        public static RibbonButton CreateElement(RibbonGroup groupToInsert, RibbonElement element)
        {
            if (element is RibbonElementButton)
            {
                return CreateElement(groupToInsert, element as RibbonElementButton);
            }
            return null;
        }

        public static RibbonButton CreateElement(RibbonGroup groupToInsert, RibbonElementButton buttonElement)
        {
            RibbonButton buttonToInsert = groupToInsert.ItemsSource.Cast<RibbonButton>().FirstOrDefault(button => button.Command == buttonElement.Command);
            if (buttonToInsert == null)
            {
                buttonToInsert = new RibbonButton
                {
                    Label = buttonElement.Label,
                    LargeImageSource = buttonElement.Image,
                    Command = buttonElement.Command
                };
                return buttonToInsert;
            }

            return null;
        }
    }
}